#include "Transaction.h"
#include <iostream>
#include <regex>

Transaction::Transaction(const std::string& date, double amount, const std::string& category,
                         const std::string& note, TransactionType type)
    : date(date), amount(amount), category(category), note(note), type(type) {}

std::string Transaction::getTypeString() const {
    return (type == TransactionType::INCOME) ? "收入" : "支出";
}

void Transaction::display() const {
    std::cout << "日期: " << date
              << " | 类型: " << getTypeString()
              << " | 金额: " << std::fixed << std::setprecision(2) << amount
              << " | 类别: " << category
              << " | 备注: " << note << std::endl;
}

bool Transaction::validateDate(const std::string& date) {
    // 验证日期格式 YYYY-MM-DD
    std::regex datePattern(R"(^\d{4}-\d{2}-\d{2}$)");
    if (!std::regex_match(date, datePattern)) {
        return false;
    }

    // 进一步验证日期的合法性
    int year, month, day;
    char dash1, dash2;
    std::istringstream iss(date);
    iss >> year >> dash1 >> month >> dash2 >> day;

    if (month < 1 || month > 12) return false;
    if (day < 1 || day > 31) return false;

    // 简单的月份天数验证
    if (month == 2) {
        bool isLeap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
        if (day > (isLeap ? 29 : 28)) return false;
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
        if (day > 30) return false;
    }

    return true;
}

bool Transaction::validateAmount(double amount) {
    return amount > 0;
}
